package net.jielan188.hd.util.common;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;

/**
 * request 请求工具
 *@author huangguogang
 *@date 2017-01-23
 */

public class RequestUtil {

  /**
   * 获取Get请求数据
  */
  public static Map<String, Object> getGetData( HttpServletRequest request ) throws IOException {

    Map<String, Object> temp = new HashMap<String, Object>();
    Enumeration<?> names = request.getParameterNames();
    while ( names.hasMoreElements() ) {
      String name = (String) names.nextElement();
      temp.put( name, getFromRequest( request, name ) );
    }
    return temp;
  }

  /**
   * 获取request请求参数中的值
  */
  public static String getFromRequest( HttpServletRequest request, String name ) {

    String val = request.getParameter( name );
    return val != null ? val.trim() : null;
  }

  /**
   * 获取Post请求数据
  */
  public static String getPostData( HttpServletRequest request ) throws IOException {

    return IOUtils.inputStreamToString( request.getInputStream(), "utf-8" );
  }

}